package de.teamskill.pux.engine;

/**
 * Port
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public abstract class Port {

	/**
	 * DATA_PUSH_WAIT_DELAY
	 */
	public static final long DATA_PUSH_WAIT_DELAY = 50;

	/**
	 * type
	 */
	private Class<?> type;

	/**
	 * remotePort
	 */
	private Port remotePort;

	/**
	 * node
	 */
	private AbstractNode node;

	/**
	 * buffer
	 */
	private DataElement buffer;

	/**
	 * name
	 */
	private String name;

	/**
	 * description
	 */
	private String description;

	/**
	 * Port Constructor
	 * 
	 * @param name
	 */
	public Port(String name) {
		this(name, null);
	}

	/**
	 * Port Constructor
	 * 
	 * @param name
	 * @param node
	 */
	public Port(String name, AbstractNode node) {
		this(name, node, null);
	}

	/**
	 * Port Constructor
	 * 
	 * @param name
	 * @param node
	 * @param remotePort
	 * @param type
	 */
	public Port(String name, AbstractNode node, Class<?> type) {
		super();
		setName(name);
		setNode(node);
		setType(type);
	}

	/**
	 * @return the type
	 */
	public Class<?> getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(Class<?> type) {
		this.type = type;
	}

	/**
	 * @return the remotePort
	 */
	public Port getRemotePort() {
		return remotePort;
	}

	/**
	 * @param remotePort
	 *            the remotePort to set
	 */
	public void setRemotePort(Port remotePort) {
		this.remotePort = remotePort;
	}

	/**
	 * @return the node
	 */
	public AbstractNode getNode() {
		return node;
	}

	/**
	 * @param node
	 *            the node to set
	 */
	public void setNode(AbstractNode node) {
		this.node = node;
	}

	/**
	 * The buffer contains the wrapped data passed through the network,
	 * including annotations and additional information. For convenient access
	 * to the data contained, use the get/setData() methods.
	 * 
	 * @return the buffer
	 */
	public DataElement getBuffer() {
		return buffer;
	}

	/**
	 * @param buffer
	 *            the buffer to set
	 */
	public void setBuffer(DataElement buffer) {
		this.buffer = buffer;
	}

	/**
	 * Convenience method to automatically wrap given data in a DataElement for
	 * transmission through the buffer
	 * 
	 * @param data
	 */
	public void setData(Object data) {
		setBuffer(new DataElement(data));
	}

	/**
	 * Convenience method to return the data contained in the buffer's data
	 * element
	 * 
	 * @return
	 */
	public Object getData() {
		return getBuffer().getData();
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * JavaBean-style getter-method for description
	 * 
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * /** JavaBean-style setter-method for description
	 * 
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * hasDataElement
	 * 
	 * @return
	 */
	public boolean hasDataElement() {
		return buffer != null;
	}

}
